//https://leetcode.cn/problems/beautiful-arrangement/
class Solution {
    int ret;
    boolean[] check;
    public int countArrangement(int n) {
        check = new boolean[n + 1];
        dfs(n, 1);
        return ret;
    }
    public void dfs(int n, int pos) {
        if(pos == n + 1) {
            ret++;
            return;
        }
        for(int i = 1; i <= n; i++) {
            if(check[i] == false && (i % pos == 0 || pos % i == 0)) {
                check[i] = true;
                dfs(n, pos + 1);
                //回溯
                check[i] = false;
            }
        }
    }
}